public with sharing class ContactController 
{
	public static String ENQUIRY_TYPE = 'enquiryType';
	
    public ContactDetails contactDetails {get;set;}
    
    public boolean submitted {get;set;}
    
    private Map<String, String> enquiryTypeLookup;
    
    public ContactController()
    {
        this.contactDetails = new ContactDetails();
        String enquiryType = Apexpages.currentPage().getParameters().get(ENQUIRY_TYPE);
        enquiryTypeLookup = new Map<String, String>();
        enquiryTypeLookup.put('LFE','Loan Referral Enquiry');
        enquiryTypeLookup.put('FAO','Franchise/Alliance Partner Opportunities');
        enquiryTypeLookup.put('C','Complaint');
        enquiryTypeLookup.put('GE','General Enquiry');
        if(CommonUtils.isNotBlank(enquiryType) && this.enquiryTypeLookup.containsKey(enquiryType))
        {
        	this.contactDetails.enquiryType = enquiryTypeLookup.get(enquiryType);
        }
    }
    
    public PageReference eventSubmitContactDetails()
    {
        validateContactDetails();
        if(this.contactDetails.getHasValidationErrors())
        {
            return null;
        }
        createEnquiry();
        this.submitted = true;
        return null;
    }
    
    public PageReference eventSubmitSpeakConsultantDetails()
    {
        validateSpeakDetails();
        if(this.contactDetails.getHasValidationErrors())
        {
            return null;
        }
        createSpeakToEnquiry();
        this.submitted = true;
        return null;
    }
    
    public List<SelectOption>getEnquiryTypes()
    {
        List<SelectOption>selectOptions = new List<SelectOption>();
        selectOptions.add(new SelectOption('', 'Select one ...'));
        for(String value : this.enquiryTypeLookup.values())
        {
        	selectOptions.add(new SelectOption(value, value));
        }
        return selectOptions;
    }
    
    private void validateContactDetails()
    {
        this.contactDetails.errors = new Set<String>();
        if(CommonUtils.isBlank(contactDetails.fullName))
        {
            contactDetails.errors.add('fullName');
        }
        if(CommonUtils.isBlank(contactDetails.enquiryType))
        {
            contactDetails.errors.add('enquiryType');
        }
        if(CommonUtils.isBlank(contactDetails.email))
        {
            contactDetails.errors.add('email');
        }
        else if(!CommonUtils.isValidEmailFormat(contactDetails.email))
        {
            contactDetails.errors.add('email');
        }
        if(CommonUtils.isBlank(contactDetails.email))
        {
            contactDetails.errors.add('phone');
        }
        else if(!CommonUtils.isValidPhoneNumber(contactDetails.phone))
        {
            contactDetails.errors.add('phone');
        }
    }
    
    private void validateSpeakDetails()
    {
        this.contactDetails.errors = new Set<String>();
        if(CommonUtils.isBlank(contactDetails.fullName))
        {
            contactDetails.errors.add('fullName');
        }
        if(CommonUtils.isBlank(contactDetails.phone))
        {
            contactDetails.errors.add('phone');
        }
        else if(!CommonUtils.isValidPhoneNumber(contactDetails.phone))
        {
            contactDetails.errors.add('phone');
        }
        if(CommonUtils.isBlank(contactDetails.postcode))
        {
            contactDetails.errors.add('postcode');
        }
        else if(!CommonUtils.isValidPostcode(contactDetails.postcode))
        {
            contactDetails.errors.add('postcode');
        }
    }
    
    private Boolean createEnquiry()
    {
        Boolean isSuccess = false;
        try
        {
            //Now create a case
            Case enquiry = new Case();
            String message = 'Name: ' + this.contactDetails.fullName +  '\n' + 
                'Phone: ' + this.contactDetails.phone + '\n' +
                    'Email: ' + this.contactDetails.email + '\n' +
                        'Message:\n' + this.contactDetails.message;
            enquiry.Description = message;
            enquiry.Subject = 
                this.contactDetails.enquiryType + ' from: ' + this.contactDetails.fullName;
            enquiry.Origin = 'Web';
            enquiry.type = this.contactDetails.enquiryType;
            insert enquiry;
            isSuccess = true;
        }
        catch(Exception e)
        {
            System.debug(e.getMessage());
        }
        return isSuccess;
    }
    
    private Boolean createSpeakToEnquiry()
    {
        Boolean isSuccess = false;
        try
        {
            //Now create a case
            Case enquiry = new Case();
            String message = 'Name: ' + this.contactDetails.fullName +  '\n' + 
                'Phone: ' + this.contactDetails.phone + '\n' + 
                    'Postcode: ' + this.contactDetails.postcode + '\n';
            enquiry.Description = message;
            enquiry.Subject = 'Speak to consultant enquiry from: ' + this.contactDetails.fullName;
            enquiry.Origin = 'Web';
            enquiry.type = 'Speak to consultant';
            insert enquiry;
            isSuccess = true;
        }
        catch(Exception e)
        {
            System.debug(e.getMessage());
        }
        return isSuccess;
    }
    

}